package org.zjh.controller.timer;

import org.zjh.constant.ImageConstant;
import org.zjh.controller.GameController;
import org.zjh.model.ComputerA;
import org.zjh.model.Player;
import org.zjh.model.RealPlayer;
import org.zjh.utils.PokerUtil;

import org.zjh.view.game.GamePanel;
import org.zjh.view.ui.GameUi;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class CallPointTimer {
    private GamePanel gamePanel;
    private GameController gameController = GameController.getInstance();
    private int countdown = 15; // 叫分时间
    private Player firstCallPointPlayer; // 记录第一个叫分的玩家
    private ActionListener actionListener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            JLabel countdownLabel =  gamePanel.getCountdownJLabel();
            Player callPointCurrentPlayer = gameController.getCallPointCurrentPlayer();
            //时间递减
            countdownLabel.setText(--countdown + "秒");

            //如果 玩家在时间内 叫分了那么就直接进入下一个玩家叫分
            if (callPointCurrentPlayer.isSuccessCallPoint()) {
                //叫分玩家叫的是否是三分 如果是 直接结束叫分阶段
                if (callPointCurrentPlayer.getCallPointResult() == 3) {
                    //提前结束叫分阶段
                    callPointSuccess(gamePanel, callPointCurrentPlayer);
                } else {
                    switchToNextPlayer(callPointCurrentPlayer);
                }
            }

            if (countdown <= 0) {
                //叫分超时 执行叫分超时方法
                callPointTimeOut(gamePanel, callPointCurrentPlayer);
            }
        }
    };
    private Timer countdownTimer = new Timer(1000, actionListener); // 倒计时定时器


    // 倒计时开始
    private void startCountdown(Player callPointCurrentPlayer) {
        countdown = 15; // 每次叫分时重新设置倒计时时间为15秒
        GamePanel gamePanel = GameUi.gameFrame.getGamePanel();
        GameController gameController = GameController.getInstance();

        countdownTimer.stop(); // 停止上一次的倒计时
        //获得倒计时标签
        JLabel countdownLabel = gamePanel.getCountdownJLabel();
        //设置倒计时标签对应位置
        gamePanel.setCountdownLabel(countdownLabel, gameController, callPointCurrentPlayer);
        //倒计时标签赋值
        countdownLabel.setText(countdown + "秒");
        //重绘游戏面板
        gamePanel.repaint();
        //开始叫分
        callPointCurrentPlayer.callPoint(callPointCurrentPlayer);

        countdownTimer.start(); // 启动倒计时
    }


    /**
     * 提前结束叫分阶段方法
     * @param gamePanel 游戏面板
     * @param callPointCurrentPlayer 当前叫分玩家
     */
    private void callPointSuccess(GamePanel gamePanel, Player callPointCurrentPlayer) {
        countdownTimer.stop();

        displayLandOwnerPoker(gamePanel, callPointCurrentPlayer);

    }


    /**
     * 叫分超时
     * @param gamePanel 游戏面板
     * @param callPointCurrentPlayer 当前叫分玩家
     */
    private void callPointTimeOut(GamePanel gamePanel, Player callPointCurrentPlayer) {
        countdownTimer.stop(); // 倒计时结束
        // 处理超时情况 自动不叫
        //实际玩家 叫分结果标签 设置为不叫
        gamePanel.getRealPlayerCallPointResultJLabel().setText("不叫");
        //给当前玩家设置叫分结果
        callPointCurrentPlayer.setCallPointResult(0);
        //将 实际玩家的 叫分按钮标签设置不可见
        gamePanel.setCallPointJLabelVisibleFalse();
        switchToNextPlayer(callPointCurrentPlayer); // 切换到下一个玩家
    }


    // 切换到下一个玩家
    private void switchToNextPlayer(Player currentPlayer) {
        GameController gameController = GameController.getInstance();
        GamePanel gamePanel = GameUi.gameFrame.getGamePanel();
        Player nextPlayer = currentPlayer.getNextPlayer();

        if (nextPlayer == firstCallPointPlayer) {

            // 所有玩家都叫过分 结束叫分阶段
            countdownTimer.stop();

            //根据叫分结果决定地主
            //获取每个玩家的叫分结果然后比较每个玩家的叫分大小 叫分大的人 是地主
            //获取电脑玩家A的叫分结果
            int computerAPlayerCallPointResult = gameController.getComputerA().getCallPointResult();
            //获取电脑玩家B的叫分结果
            int computerBPlayerCallPointResult = gameController.getComputerB().getCallPointResult();
            //获取实际玩家的叫分结果
            int realPlayerCallPointResult = gameController.getPlayer().getCallPointResult();
            //比较 获得最高分玩家
            Player player = compareAllPlayerCalPoint(computerAPlayerCallPointResult, realPlayerCallPointResult, computerBPlayerCallPointResult);
            if (player == null) { //都不叫
                //将游戏面板中的叫分结果标签重置
                gamePanel.resetCallPointResultJLabelAndCountDownJLabel();
                gamePanel.getMultipleJLabel().setVisible(false);
                //返回值为空 代表都不叫 那么重新发牌
                PokerUtil.reDealPoker();
            } else { //有叫分
                displayLandOwnerPoker(gamePanel, player);
                //叫分定时器停止
                countdownTimer.stop();
            }

            return;
        }
        gameController.setCallPointCurrentPlayer(nextPlayer);
        startCountdown(nextPlayer); // 开始下一个玩家的倒计时
    }

    /**
     * 展示地主牌
     * @param gamePanel 游戏面板
     * @param player 玩家
     */
    private void displayLandOwnerPoker(GamePanel gamePanel, Player player) {
        //记录地主玩家
        gameController.setLandOwnerPlayer(player);
        //显示明牌
        gamePanel.getFaceUpJlabel().setVisible(true);
        //将游戏面板中的叫分标签重置
        gamePanel.resetCallPointResultJLabelAndCountDownJLabel();
        //获得游戏身份头像
        getAvatar(player, gamePanel);
        gamePanel.setAvatarVisibleTrue();
        //叫分结束 显示 地主牌
        PokerUtil.showAndGetDZPoker(GameController.getInstance().getLandOwnerPokerList());
        Timer delayTimer = new Timer(1250, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //获得地主手牌并且重新排序
                PokerUtil.getLandOwnerPoker(player);
                new Timer(2250, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        //开启出牌定时器
                        gameController.getDiscardPokersTimer().start(player);
                        ((Timer) e.getSource()).stop();
                        //机器人标签可见
                        GameUi.gameFrame.getGamePanel().getMachineHostJLabel().setVisible(true);
                    }
                }).start();
                ((Timer) e.getSource()).stop();
            }
        });
        delayTimer.start();

    }

    /**
     * 开始
     */
    public void start() {
        gamePanel = GameUi.gameFrame.getGamePanel();
        // 游戏开始时，初始化第一个玩家并启动倒计时
        firstCallPointPlayer = GameController.getInstance().getCallPointCurrentPlayer();
        startCountdown(firstCallPointPlayer);
    }


    /**
     * 比较所有玩家的叫分结果
     * @return 最高叫分的玩家
     */
    private Player compareAllPlayerCalPoint(int computerAPlayerCallPointResult, int realPlayerCallPointResult, int computerBPlayerCallPointResult) {


        //如果所有玩家都不叫那么返回空
        if (computerAPlayerCallPointResult == 0 && realPlayerCallPointResult == 0 && computerBPlayerCallPointResult == 0) {
            return null;
        }
        //返回最高得分的玩家

        if (computerAPlayerCallPointResult > computerBPlayerCallPointResult) {
            if (computerAPlayerCallPointResult > realPlayerCallPointResult) {
                return GameController.getInstance().getComputerA();
            } else {
                return GameController.getInstance().getPlayer();
            }
        } else {
            if (computerBPlayerCallPointResult > realPlayerCallPointResult) {
                return GameController.getInstance().getComputerB();
            } else {
                return GameController.getInstance().getPlayer();
            }
        }
    }


    /**
     * 获得对应头像
     * @param player 玩家
     * @param gamePanel 游戏面板
     */
    private void getAvatar(Player player, GamePanel gamePanel) {
        if (player instanceof RealPlayer) {
            gamePanel.getRealPlayerAvatar().setIcon(ImageConstant.LANDOWNER_IMAGE);
            gamePanel.getComputerAAvatar().setIcon(ImageConstant.FARMER_IMAGE);
            gamePanel.getComputerBAvatar().setIcon(ImageConstant.FARMER_IMAGE);
        } else if (player instanceof ComputerA) {
            gamePanel.getComputerAAvatar().setIcon(ImageConstant.LANDOWNER_IMAGE);
            gamePanel.getRealPlayerAvatar().setIcon(ImageConstant.FARMER_IMAGE);
            gamePanel.getComputerBAvatar().setIcon(ImageConstant.FARMER_IMAGE);
        } else {
            gamePanel.getComputerBAvatar().setIcon(ImageConstant.LANDOWNER_IMAGE);
            gamePanel.getRealPlayerAvatar().setIcon(ImageConstant.FARMER_IMAGE);
            gamePanel.getComputerAAvatar().setIcon(ImageConstant.FARMER_IMAGE);
        }
    }

    public Timer getCountdownTimer() {
        return countdownTimer;
    }
}
